<template>
  <div class="change-password-container">
    <el-card class="change-password-box">
      <div class="logo">
        <img :src="logoUrl" alt="Logo" class="logo-image">
      </div>
      <h2 class="change-password-title">修改密码</h2>

      <el-form ref="passwordForm" :model="passwordForm" :rules="passwordRules" label-position="top">
        <el-form-item label="邮箱" prop="email">
          <el-input
              v-model="passwordForm.email"
              placeholder="请输入注册邮箱"
              prefix-icon="el-icon-message"
              clearable
              class="custom-input"
          ></el-input>
        </el-form-item>

        <el-form-item label="原密码" prop="oldPassword">
          <el-input
              v-model="passwordForm.oldPassword"
              type="password"
              placeholder="请输入当前密码"
              prefix-icon="el-icon-lock"
              show-password
              class="custom-input"
          ></el-input>
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input
              v-model="passwordForm.newPassword"
              type="password"
              placeholder="请输入新密码"
              prefix-icon="el-icon-lock"
              show-password
              class="custom-input"
          ></el-input>
        </el-form-item>

        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
              v-model="passwordForm.confirmPassword"
              type="password"
              placeholder="请再次输入新密码"
              prefix-icon="el-icon-lock"
              show-password
              class="custom-input"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
              type="success"
              @click="handleChangePassword"
              class="submit-btn"
              :loading="loading"
          >
            确认修改
          </el-button>
        </el-form-item>

        <div class="action-links">
          <el-link type="primary" @click="goBack" class="action-link">返回登录</el-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';
import logoImage from '@/assets/logo.png';

export default {
  name: 'ChangePassword',
  data() {
    const validateEmail = (rule, value, callback) => {
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!value) {
        callback(new Error('请输入邮箱'));
      } else if (!emailRegex.test(value)) {
        callback(new Error('请输入有效的邮箱地址'));
      } else {
        callback();
      }
    };

    const validateNewPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入新密码'));
      } else if (value.length < 8) {
        callback(new Error('密码长度不能少于8位'));
      } else {
        callback();
      }
    };

    const validateConfirmPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请再次输入新密码'));
      } else if (value !== this.passwordForm.newPassword) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    };

    return {
      logoUrl: logoImage,
      loading: false,
      passwordForm: {
        email: '',
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      passwordRules: {
        email: [
          { validator: validateEmail, trigger: 'blur' }
        ],
        oldPassword: [
          { required: true, message: '请输入当前密码', trigger: 'blur' }
        ],
        newPassword: [
          { validator: validateNewPassword, trigger: 'blur' }
        ],
        confirmPassword: [
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleChangePassword() {
      this.$refs.passwordForm.validate(async (valid) => {
        if (!valid) return;

        this.loading = true;
        try {
          const response = await axios.post('http://47.120.40.5:5000/api/user/changePwd', {
            email: this.passwordForm.email,
            current_password: this.passwordForm.oldPassword,
            new_password: this.passwordForm.newPassword
          });

          if (response.data.success) {
            this.$message.success('密码修改成功！');
            this.$router.push('/');
          }
        } catch (error) {
          if (error.response) {
            switch (error.response.status) {
              case 400:
                if (error.response.data.message.includes('长度不足')) {
                  this.$message.error('新密码长度至少为8个字符');
                } else {
                  this.$message.error('请检查输入信息是否正确');
                }
                break;
              case 401:
                this.$message.error('当前密码不正确');
                break;
              case 404:
                this.$message.error('用户不存在或账户未激活');
                break;
              case 500:
                this.$message.error('服务器错误，请稍后重试');
                break;
              default:
                this.$message.error('修改失败，请稍后重试');
            }
          } else {
            this.$message.error('网络错误，请检查网络连接');
          }
        } finally {
          this.loading = false;
        }
      });
    },
    goBack() {
      this.$router.push('/');
    }
  }
}
</script>

<style scoped>
.change-password-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background-color: #f0f2f5;
  background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.change-password-box {
  width: 100%;
  max-width: 450px;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
  border: none;
  background-color: rgba(255, 255, 255, 0.95);
}

.logo {
  text-align: center;
  margin-bottom: 20px;
}

.logo-image {
  height: 70px;
  width: auto;
  max-width: 100%;
  object-fit: contain;
  border-radius: 40px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.change-password-title {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 30px;
  font-weight: 600;
  font-size: 22px;
  letter-spacing: 0.5px;
}

.custom-input {
  border-radius: 8px;
}

.custom-input >>> .el-input__inner {
  height: 44px;
  line-height: 44px;
}

.submit-btn {
  width: 100%;
  margin-top: 10px;
  height: 44px;
  font-size: 16px;
  border-radius: 8px;
  background-color: #67c23a;
  border-color: #67c23a;
  transition: all 0.3s;
}

.submit-btn:hover {
  background-color: #85ce61;
  border-color: #85ce61;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(103, 194, 58, 0.3);
}

.action-links {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.action-link {
  font-size: 14px;
}

/* 手机端特定样式 */
@media (max-width: 480px) {
  .change-password-box {
    padding: 25px 15px;
    border-radius: 10px;
  }

  .logo-image {
    height: 60px;
    border-radius: 40px;
  }

  .change-password-title {
    font-size: 20px;
    margin-bottom: 25px;
  }

  .custom-input >>> .el-input__inner {
    height: 42px;
    line-height: 42px;
    font-size: 14px;
  }

  .submit-btn {
    height: 42px;
    font-size: 15px;
  }

  .action-link {
    font-size: 13px;
  }
}
</style>